package com.flf.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.flf.entity.Room;
import com.flf.service.RoomService;

@Controller
@RequestMapping(value="/room")
public class RoomController {

	@Resource
	private RoomService roomService;
	
	/**
	 * 显示菜单列表
	 * @param model
	 * @return
	 */
	@RequestMapping
	public String list(Model model){
		List<Room> roomList = roomService.listAllRoom();
		model.addAttribute("roomList", roomList);
		return "room";
	}
	
	/**
	 * 请求新增菜单页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add")
	public String toAdd(Model model){
		return "room_info";
	}
	
	/**
	 * 请求编辑菜单页面
	 * @param roomId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/edit")
	public String toEdit(@RequestParam Integer roomId,Model model){
		Room room = roomService.getRoomById(roomId);
		model.addAttribute(room);
		return "room_info";
	}
	
	/**
	 * 保存菜单信息
	 * @param room
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/save")
	public String save(Room room,Model model){
		roomService.insertRoom(room);
		model.addAttribute("msg", "success");
		return "save_result";
	}
	
	
	/**
	 * 删除菜单
	 * @param roomId
	 * @param out
	 */
	@RequestMapping(value="/del")
	public void delete(@RequestParam Integer roomId,PrintWriter out){
		roomService.deleteRoom(roomId);
		out.write("success");
		out.flush();
		out.close();
	}
}
